package com.training.food.controller;

import com.training.food.Domain.Result;
import com.training.food.Domain.SlaughterCheck;
import com.training.food.Mapper.RecordinfoMapper;
import com.training.food.Mapper.RetailMapper;
import com.training.food.Mapper.WSmarketMapper;
import com.training.food.pojo.*;
import com.training.food.service.*;
import com.training.food.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SourceController {
    @Autowired
    WSmarketService wsmarketService;
    @Autowired
    RetailService retailService;
    @Autowired
    SlaughterService slaughterService;
    @Autowired
    WSmarketMapper wsmarketMapper;
    @Autowired
    RecordinfoMapper recordinfoMapper;
    @Autowired
    InitialpurService initialpurService;
    @Autowired
    RecordService recordService;
    @Autowired
    RetailMapper retailMapper;
    @Autowired
    FarmService farmService;
    @Resource
    CheckService checkService;
    @PostMapping("/food/source/customer")
    CommonResult source(@RequestBody Retail retail){

        retail=retailService.selectById(retail);
        Recordinfo recordinfoRetail =recordinfoMapper.find(retail.getUsername());
        WSmarket wsmarket=new WSmarket();
        wsmarket.setWSmarketId(retail.getWSmarketID());
        WSmarket WSmarket1 =wsmarketMapper.select(wsmarket);
        Recordinfo recordinfoWSmarket=recordinfoMapper.find(WSmarket1.getUsername());

        List<Initialpur>listInitialpur=initialpurService.list(WSmarket1.getWSmarketId());
        List<Slaughter> Slaughters =slaughterService.slaughterInfos(listInitialpur);
        List<Recordinfo>recordinfoSlaughters=recordService.lists(Slaughters);
        List<SlaughterCheck>slaughterChecks=new ArrayList<SlaughterCheck>();
        for (Slaughter slaughter:Slaughters) {
            SlaughterCheck slaughterCheck=new SlaughterCheck();
            slaughterCheck.setSlaughter(slaughter);
            List<Check>checks=checkService.checks(slaughter);
            slaughterCheck.setChecks(checks);
            slaughterChecks.add(slaughterCheck);
        }

        List<Farm> Farms =farmService.lists(Slaughters);
        Result r=new Result();
       r.setRecordinfoRetail(recordinfoRetail);r.setRecordinfoWSmarket(recordinfoWSmarket);r.setRecordinfoSlaughter(recordinfoSlaughters);
       r.setRetail(retail);r.setWsmarket(WSmarket1);r.setSlaughterChecks(slaughterChecks);
        r.setFarms(Farms);
        return CommonResult.success(r);

    }
    @PostMapping("/food/source/freezeFood")
    CommonResult freezeFood(@RequestBody Retail retail){




        if(retail!=null) {
            retail=retailService.selectById(retail);
            retail.setState("2");
            retailService.update(retail);

            WSmarket wsmarket=new WSmarket();
            wsmarket.setWSmarketId(retail.getWSmarketID());
            WSmarket WSmarket1 =wsmarketMapper.select(wsmarket);
            if(WSmarket1!=null) {
                WSmarket1.setState("2");
                wsmarketService.update(WSmarket1);

                List<Initialpur>listInitialpur=initialpurService.list(WSmarket1.getWSmarketId());
                List<Slaughter> Slaughters =slaughterService.slaughterInfos(listInitialpur);
                if(Slaughters.size()!=0) {
                    for (Slaughter slaughter : Slaughters) {
                        slaughter.setState("2");
                        slaughterService.update(slaughter);
                    }

                }
            }
        }

        return CommonResult.success("成功了吧");

    }
}
